/**
 * @Auther: ayo
 * @Date: 2018/8/22 15:57
 * @Description: toast工具类
 */
import { Injectable } from '@angular/core';
import { ToastController } from 'ionic-angular';

@Injectable()
export class ToastProvider {
    constructor(private toastCtrl: ToastController) {}

    /*************** toast start ******************** */

    // 成功
    success(message, position: string = 'top'): Promise<any> {
        return this.showToast(message, 'blue', 2000, position);
    }

    // 警告
    warning(message, position: string = 'top'): Promise<any> {
        return this.showToast(message, 'yellow', 2000, position);
    }

    // 错误
    error(message, position: string = 'top'): Promise<any> {
        return this.showToast(message, 'red', 2000, position);
    }

    showToast(message: string, color: string = '', duration: number = 2000, position: string = 'top'): Promise<any> {
        let cssClass = '';
        if (!!color) {
            cssClass = 'toast-' + color;
        }
        const toast = this.toastCtrl.create({
            message: message, duration: duration, position: position, cssClass: cssClass
        });
        return toast.present(toast);
    }

    // demo2
    /**
     * toast短暂提示   (支持自定义)
     * http://ionicframework.com/docs/api/components/toast/ToastController/
     * @param {string} content
     * @param {string} position    //"top", "middle", "bottom".
     * @param {boolean} showCloseButton
     * @param {string} closeButtonText
     * @param {string} cssClass
     * @param {number} duration
     * @param {function} callback
     */
    toast(content: string, position: string = 'middle', showCloseButton: boolean = false, closeButtonText: string = '关闭', cssClass: string = '', duration: number = 3000, callback?) {
        // position: "top", "middle", "bottom".
        // dismissOnPageChange: 当页面变化时是否dismiss
        const toast = this.toastCtrl.create({
            message: content,
            duration: 3000,
            position: position,
            cssClass: cssClass,
            showCloseButton: showCloseButton,
            closeButtonText: closeButtonText,
            dismissOnPageChange: false
        });
        toast.onDidDismiss(() => {});
        toast.present();
        if (callback) {
            callback();
        }
    }
}
